package com.wx.main.interceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.Connection;
import java.util.Properties;

/**
 * @author wangxun
 * mybatis 插件使用:
 * 1.实现 Interceptor 接口
 * 2.配置注解，@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}
 * 在执行 Executor 类的参数为 {MappedStatement.class, Object.class} 的 update 方法时，会先执行拦截器中的 intercept 方法
 */
@Intercepts({
  @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
  @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
  @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})/*,
  @Signature(type = ResultSetHandler.class, method = "", args = {}),
  @Signature(type = ParameterHandler.class, method = "", args = {})*/
})
public class ExamplePlugin2 implements Interceptor {

  private Properties properties = new Properties();

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    System.out.println(1);
    Object returnObject = invocation.proceed();
    System.out.println(2);
    return returnObject;
  }

  @Override
  public void setProperties(Properties properties) {
    this.properties = properties;
  }
}
